/**
 * Created by zoulu on 15/10/14.
 */

var Manage = {
	init: function() {
//		Manage.get_data();
		$(".pagination li a").click(function() {
			var page = $(this).attr("page");
			var size = 10;
			Manage.get_data(page, size);
			Common.init_pagination(page);
		});
		$("#from").find("button").click(function(){
			$(this).css("background-color","#ECD4D4").attr("is_select",1).siblings("button").css("background-color","#fff").attr("is_select",0);
			Manage.get_data();
		});

	},
	get_data: function(page, size) {
		if (page == undefined) {
			page = 0;
		}
		if (size == undefined) {
			size = 10;
		}
		var user_id = $("#user_id").val();
		if (user_id == "") {
			alert("请输入用户ID");
			return;
		}
		var form_uri = $("#from").find("button[is_select=1]").attr("form_uri");
		if (form_uri == undefined || form_uri == "") {
			alert("请选择查询课程的方式");
			return;
		}
//		if (form_uri == '/users/lessons/list'){
//			ajax(form_uri, {
//				"teacher_id": user_id,
//				"page": page,
//				"size": size
//			}, callback_init_table);
//		} else{
			ajax(form_uri, {
				"user_id": user_id,
				"page": page,
				"size": size
			}, callback_init_table);
//		}
		
	},

	init_table: function(result) {
		if (result != null && result.data != null) {
			var tbody = $("#data_table tbody");
			tbody.empty();
			
			var lessons = [];
			if (result.data.jobs) {
				var jobs = result.data.jobs;
				for (var m = 0; m < jobs.length; m++) {
					var job = jobs[m];
					lessons = lessons.concat(job.lessons);
				}
			} else if(result.data.lessons) {
				lessons = result.data.lessons;
			}
			var schools = result.data.schools == undefined ? [] : result.data.schools;
			var users = result.data.users == undefined ? [] : result.data.users;
			var classes = result.data.classes == undefined ? [] : result.data.classes;

			var schoolsmap = new Map();
			for(var i=0;i<schools.length;i++){
				schoolsmap.put(schools[i].id,schools[i].name);
			}
			var usersmap = new Map();
			for(var i=0;i<users.length;i++){
				usersmap.put(users[i].id,users[i].name);
			}
			var classesmap = new Map();
			for(var i=0;i<classes.length;i++){
				classesmap.put(classes[i].id,classes[i].name);
			}
			var trs = "";
			for (var i = 0; i < lessons.length; i++) {
				var lesson = lessons[i];
				trs += ("<tr>");
				var level = "不限";
				if (lesson.level == 1) {
					level = "初级";
				}
				if (lesson.level == 2) {
					level = "中级";
				}
				if (lesson.level == 3) {
					level = "高级";
				}
				var name = ("<td>" + lesson.name + "</td>");

				var owner_name = ("<td>" + (usersmap.get(lesson.owner_id) == undefined ? "未知" : usersmap.get(lesson.owner_id))  + "</td>");
				var school_name = ("<td>" + (schoolsmap.get(lesson.school_id) == undefined ? "未知" : schoolsmap.get(lesson.school_id))  + "</td>");
				var job_id = ("<td>" + lesson.job_id + "</td>");
				var class_name = ("<td>" + (classesmap.get(lesson.class_id) == undefined ? "未知" : classesmap.get(lesson.class_id)) + "</td>");


				var start_at = ("<td>" + formatDate(lesson.start_at) + "</td>");
				var end_at = ("<td>" + formatDate(lesson.end_at) + "</td>");

				var lessonlevel = ("<td>" + level + "</td>");
				var teach_fee = ("<td>" + lesson.teach_fee + "</td>");
				var fee = ("<td>" + lesson.fee + "</td>");
				var current_fee = ("<td>" + lesson.current_fee + "</td>");

				trs += (name + owner_name + school_name + job_id + class_name + start_at + end_at + lessonlevel + teach_fee + fee + current_fee);
				trs += ("</tr>");
			}
			if (lessons.length == 0) {
				trs += ("<tr>");
				trs += ("<td colspan='11'>未查询到任何结果</td>");
				trs += ("</tr>");
			}
			tbody.append(trs);

		}
	}
};

function callback_init_table(result) {
	Manage.init_table(result);
}

function callback_init_class(result) {
	Manage.init_class(result);
}

$(function() {
	Manage.init();
})